package stack;

import java.util.Stack;

public class isValid {
    public boolean isValid (String s) {
        // write code here
        Stack<Character> stack = new Stack<>();
        for(int i = 0;i < s.length();i++) {
            if(s.charAt(i) == '(' || s.charAt(i) == '['||s.charAt(i)=='{') {
                stack.push(s.charAt(i));
            }else {
                if(stack.isEmpty()) {
                    return false;
                }

                char tem = stack.pop();
                if(s.charAt(i) == ')'){
                    if(tem != '(') {
                        return false;
                    }
                }else if(s.charAt(i)==']'){
                    if(tem != '[') {
                        return false;
                    }
                }else{
                    if(tem != '{') {
                        return false;
                    }
                }

            }
        }
        if(!stack.isEmpty()) {
            return false;
        }
        return true;
    }
}
